"use strict"
const nunjucks = require('nunjucks');
const path = require('path');
//nunjucks.configure('views');

function createEnv(path, opts) {
    console.log(path);
    var
        autoescape = opts.autoescape === undefined ? true : opts.autoescape,
        noCache = opts.noCache || false,
        watch = opts.watch || false,
        throwOnUndefined = opts.throwOnUndefined || false,
        env = new nunjucks.Environment(
            //注意路径 一个工作区建了多个文件夹导致相当路径发生变化'koa-nunjucks/views'
            new nunjucks.FileSystemLoader(path, {
                noCache: noCache,
                watch: watch,
            }), {
            autoescape: autoescape,
            throwOnUndefined: throwOnUndefined
        });
    if (opts.filters) {
        for (var f in opts.filters) {
            env.addFilter(f, opts.filters[f]);
        }
    }
    return env;
}

var env = createEnv(path.resolve(__dirname, 'views'), {
    watch: true,
    filters: {
        hex: function (n) {
            return '0x' + n.toString(16);
        }
    }
});


var name = 'xiaoming';
var fruits = ['apple', 'banana', 'pear'];
var s = env.render('hello.html', { name: name, fruits: fruits });
console.log(s);
